// Copyright 2021 Guoyoko. All Rights Reserved.

#include "SWLevelEditorModule.h"
#include "Modules/ModuleManager.h"
#include "SWLevelEditorCommands.h"
#include "Widgets/Docking/SDockTab.h"
#include "SSWLevelEidtor.h"


const FName FSWLevelEditorTabIds::ToolBar(TEXT("ToolBar"));
const FName FSWLevelEditorTabIds::Viewport(TEXT("Viewport"));
const FName FSWLevelEditorTabIds::PlaceActorsBrowser(TEXT("PlaceActorsBrowser"));
const FName FSWLevelEditorTabIds::SelectionDetails(TEXT("SelectionDetails"));

void FSWLevelEditorModule::StartupModule()
{
	FSWLevelEditorCommands::Register();

	BindLevelEditorCommands();

	FGlobalTabmanager::Get()->RegisterTabSpawner("SWLevelEditor", FOnSpawnTab::CreateRaw(this, &FSWLevelEditorModule::SpawnLevelEditor))
		.SetDisplayName(NSLOCTEXT("SWLevelEditor", "SWLevelEditorTab", "SW Level Editor"));

}

void FSWLevelEditorModule::ShutdownModule()
{
}

void FSWLevelEditorModule::SetLevelEditorTabManager(const TSharedPtr<SDockTab>& OwnerTab)
{
	if (OwnerTab.IsValid())
	{
		LevelEditorTabManager = FGlobalTabmanager::Get()->NewTabManager(OwnerTab.ToSharedRef());

	}
}

TSharedPtr<class FTabManager> FSWLevelEditorModule::GetLevelEditorTabManager() const
{
	return LevelEditorTabManager;
}

void FSWLevelEditorModule::BindLevelEditorCommands()
{
	CommandActionsList = MakeShareable(new FUICommandList);
	const FSWLevelEditorCommands& Commands = FSWLevelEditorCommands::Get();

	CommandActionsList->MapAction(Commands.ScreenShot, FExecuteAction::CreateStatic(&FSWLevelEditorActionCallbacks::ScreenShot));

}

TSharedRef<SDockTab> FSWLevelEditorModule::SpawnLevelEditor(const FSpawnTabArgs& InArgs)
{
	// Create MainDock
	TSharedRef<SDockTab> MainDockTab = SNew(SDockTab)
		.TabRole(ETabRole::MajorTab)
		.OnCanCloseTab_Lambda([]() ->bool {return false; }) // dont allowed close maindock
		//.Label //Set Label as map name. Do it after map loaded.
		;
	FGlobalTabmanager::Get()->SetMainTab(MainDockTab);
	
	TSharedPtr<SSWLevelEditor> LevelEditorTmp;
	MainDockTab->SetContent(SAssignNew(LevelEditorTmp, SSWLevelEditor));
	LevelEditorTmp->Initialize(MainDockTab);
	
	return MainDockTab;
}

IMPLEMENT_MODULE(FSWLevelEditorModule, SplitWindowsLevelEditor);
